<?php
/**
 *
 *
 */
namespace Yz\modules\Users;

use Yz_Array,
    Yz_Hash;

class Controller_Login
    extends \Yz_Controller_Templated
{

    protected $loginFailure = false;
    protected $captchaFailure = false;
    protected $fromURL;




    /**
     *
     */
    public function doGet(Yz_Array $args)
    {
        debug();
        $this
            ->checkAccess()
        ;

        $this->fromURL = $this->getRequest()->fetchArgument('path');

        $template = $this->selectTemplate();

        $template->fromURL = $this->fromURL;
        $template->loginFailure = $this->loginFailure;
        $template->captchaFailure = $this->captchaFailure;
        $template->captchaEnabled = $this->captchaEnabled();

        return $this->outputTemplate($template);
    }


    /**
     *
     *
     */
    public function doPOST(Yz_Array $args, Yz_Hash $post)
    {
        debug();
        $this
            ->checkAccess()
            ->testCaptcha($post)
        ;

        $user = \Yz_User_Default::getInstance();

        $login = $post->offsetGet('login');
        $password = $post->offsetGet('password');
        $user->authenticate($login, $password);

        $rq = $this->getRequest();
        $url = $rq->fetchArgument("path", $rq->getWebPrefix());
        $this->getView()
            ->setRedirect( $url )
        ;
        return $this;
    }


    /**
     *  @todo
     */
    public function doError(\Yz_Exception $ex)
    {
        debug();

        switch (true) {
            case $ex instanceof Exception_WrongCaptcha:
                $this->captchaFailure = true;
                break;
            case $ex instanceof \Yz_Exception_ItemNotFound:
            case $ex instanceof \Yz_User_Exception:
                $this->loginFailure = true;
                break;
            default:
                break;
        }
        return $this->doGet(new \Yz_Array);
    }


    /**
     *
     */
    protected function checkAccess()
    {
        debug();

        $user = \Yz_User_Default::getInstance();
        if ($user->isAuthenticated()) {
            throw new \Yz_Access_Exception("User '$user->login' can not login unless logged out");
        }

        return $this;
    }


    /**
     *
     *  @return bool
     *
     */
    protected function testCaptcha(\Yz_Hash $post)
    {
        debug();

        if ($this->captchaEnabled()) {
            $code = $post->offsetGet('captcha');
            if (null === $code) {
                throw new Exception_WrongCaptcha;
            }
            $captModule = \Yz::selectModule("captcha");
            $captcha = $captModule->selectModel("Captcha");
            try {
                $captcha->verifyCode($code);
            } catch (\Yz\modules\Captcha\Exception_CodeIsWrong $ex) {
                throw new Exception_WrongCaptcha;
            } catch (\Yz_Exception_InvalidArgument $ex) {
                throw new Exception_WrongCaptcha;
            }
        }
        return $this;
    }

    /**
     *  @return bool
     *  @todo the config value is definitely the concern of the Captcha module
     */
    protected function captchaEnabled()
    {
        debug();
        return \Yz::getConfig()->fetchBoolean("users.login.captcha.enabled");
    }

}
